package com.fcscs.cservice.client.main.configuration.restaurant;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.controller.BookingServiceController;
import com.fcscs.cservice.client.common.model.configuration.restaurant.RestaurantModelData;
import com.fcscs.cservice.domain.RestaurantConfig;
import com.fcscs.cservice.domain.model.RestaurantConfiguration;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class MySimpeGridPanel extends SimpleGridPanel<RestaurantModelData> implements
FormSubmitListener<RestaurantConfiguration>, ModelSelectListener<RestaurantModelData>{
	private RestaurantConfigDialog restaurantConfigDialog;
	private RestaurantModelData currentModel;

	public MySimpeGridPanel(String heading, ModelSelectListener<RestaurantModelData> l) {
		super(heading, l, (Object) null);
		setSize(720, 530);
		setCollapsible(false);
	}

	@Override
	protected ColumnModel createColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

		ColumnConfig columnConfig = CoreUtil.createNonSortableColumnConf(CoreConstants.FULLTNAME,
				CServiceConstants.CSERVICE_CONSTANTS.restaurantName(), 200);
		columns.add(columnConfig);

		columnConfig = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,
				CoreConstants.CORE_CONSTANTS.action(), 120);
		columnConfig.setRenderer(new GridCellRenderer<RestaurantModelData>() {

			@Override
			public Object render(RestaurantModelData model, String property,
					com.extjs.gxt.ui.client.widget.grid.ColumnData config, int rowIndex, int colIndex,
					ListStore<RestaurantModelData> store, Grid<RestaurantModelData> grid) {
				HorizontalPanel horiPanel = new HorizontalPanel();
				horiPanel.setId("MySimpeGridPanel_horiPanel_id");
				horiPanel.setHorizontalAlign(HorizontalAlignment.LEFT);

				HighLightIconButton editBtn = createEditBtn(model, store);
				editBtn.setId("MySimpeGridPanel_editBtn_id");
				editBtn.setTitle(CServiceConstants.CSERVICE_CONSTANTS.restaurantConf());
				editBtn.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "5px");
				horiPanel.add(editBtn);
				return horiPanel;
			}
		});
		columns.add(columnConfig);
		ColumnModel columnModel = new ColumnModel(columns);

		return columnModel;
	}

	protected HighLightIconButton createEditBtn(final RestaurantModelData model,
			ListStore<RestaurantModelData> store) {
		
		final long propId = model.getEntity().getPropertyId();
		final long restaId = model.getEntity().getId();
		currentModel = model;
		HighLightIconButton iconBtn = new HighLightIconButton(CServiceConstants.CSS_CONFIGURATION_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						BookingServiceController.getInstance().getRestaurantConfig(propId, restaId,
								new AsyncCallbackListener<List<RestaurantConfig>>() {

							@Override
							public void onLogin(String username) {
								BookingServiceController.getInstance().getRestaurantConfig(propId, restaId,this);
							}

							@Override
							public void onFailure(Throwable caught) {
								MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
										CoreConstants.CORE_CONSTANTS.system(),
										ShareGWTConstants.SHARE_GWT_CONSTANTS.error()), CoreConstants.CORE_CONSTANTS
										.systemError(), null);
							}

							@Override
							public void onSuccess(List<RestaurantConfig> result) {
								if (null != result) {
									RestaurantConfig rc;
									if (0 != result.size()) {
										rc = result.get(0);
									}else {
										rc = null;
									}
									restaurantConfigDialog = RestaurantConfigDialog.getInstance(MySimpeGridPanel.this, rc, model);
									restaurantConfigDialog.show();
								}
							}
						});
					}
				});
		iconBtn.setId("DepartmentGridPanel_editbtn_id");
		iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.edit());
		return iconBtn;
	}
	
	@Override
	public void formSubmitCallBack(FcsFormPanel<RestaurantConfiguration> form, SubmitType submitType) {
		RestaurantConfiguration rc = new RestaurantConfiguration();
		restaurantConfigDialog.getFormPanel().populateEntity(rc);
		updateRestaurantConfig(rc);		
	}
	
	public void updateRestaurantConfig(final RestaurantConfiguration resc){
		BookingServiceController.getInstance().saveRestaurantConfig(resc, new AsyncCallbackListener<RestaurantConfiguration>() {
			
			@Override
			public void onLogin(String username) {
				BookingServiceController.getInstance().saveRestaurantConfig(resc,this);
				
			}
			
			@Override
			public void onSuccess(RestaurantConfiguration result) {
				if (null != result) {
					replaceModel(currentModel,new RestaurantModelData(result.getRestaurant()));
					restaurantConfigDialog.hide();
					CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.saved());
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
						CoreConstants.CORE_CONSTANTS.system(),
						ShareGWTConstants.SHARE_GWT_CONSTANTS.error()), CoreConstants.CORE_CONSTANTS
						.systemError(), null);
			}
		});
	}


	@Override
	public void onModelClick(RestaurantModelData model) {
		
	}

	@Override
	public void onModelDoubleClick(RestaurantModelData model) {
		
	}

	@Override
	public void onModelEnterPress(RestaurantModelData model) {
		
	}
}